package com.future;

import java.util.LinkedList;

/**
 * Description: NC170 最长不含重复字符的子字符串
 *
 * @author weiruibai.vendor
 * Date: 2022/7/29 17:18
 */
public class Solution_NC170 {

    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        int N = s.length();
        if (N == 1) {
            return 1;
        }
        int ans = 1;
        char[] chars = s.toCharArray();
        LinkedList<Character> linked = new LinkedList<>();
        for (int i = 0; i < N; i++) {
            if (linked.contains(chars[i])) {
                ans = Math.max(ans, linked.size());
                while (linked.contains(chars[i])) {
                    linked.pollLast();
                }
            }
            linked.addFirst(chars[i]);
        }
        return Math.max(ans, linked.size());
    }
}
